package exp1dtree;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintStream;

import exp1dtree.dtree.DTree;

public final class Option {

	private String dstFileName;

	private String trainFileName;

	private String testFileName;

	private InputStream trainInput = Driver.class.getResourceAsStream("/data/census-income.data.10000");

	private InputStream testInput = Driver.class.getResourceAsStream("/data/census-income.test.5000");

	private PrintStream output = System.out;

	private PrintStream err = System.out;
	
	private int fraction = 50;
	
	private int ratio = 4;
	
	private int validation = 0;
	
	private DTree.Strategy strategy;

	public Option(String[] args) {
		if (args.length % 2 != 0) {
			output.println(usage());
			System.exit(0);
		}
		for (int i = 0; i < args.length; i++) {
			if (args[i].equals("-o")) {
				dstFileName = args[++i];
				try {
					output = new PrintStream(new FileOutputStream(dstFileName));
				} catch (FileNotFoundException e) {
					err.println("Can not open file " + dstFileName
							+ " for write");
					System.exit(1);
				}
			} else if (args[i].equals("-d")) {
				trainFileName = args[++i];
				try {
					trainInput = new FileInputStream(trainFileName);
				} catch (FileNotFoundException e) {
					err.println("File " + trainFileName + " not found");
					System.exit(1);
				}
			} else if (args[i].equals("-t")) {
				testFileName = args[++i];
				try {
					testInput = new FileInputStream(testFileName);
				} catch (FileNotFoundException e) {
					err.println("File " + testFileName + " not found");
					System.exit(1);
				}
			} else if (args[i].equals("-f")){
				fraction = Integer.parseInt(args[++i]);
			} else if (args[i].equals("-r")){
				ratio = Integer.parseInt(args[++i]);
			} else if (args[i].equals("-p")){
				String s = args[++i].toUpperCase();
				if (s.equals("NO")) {
					validation = 0;
				} else if (s.equals("PRE")) {
					validation = 40;
				} else if (s.equals("POST")) {
					validation = 40;
				} else {
					output.println(usage());
					System.exit(0);				
				}
				strategy = DTree.Strategy.valueOf(s);
			} else {
				output.println(usage());
				System.exit(0);
			}
		}
		
		if (validation + fraction > 100) {
			output.println("Incorrect parameters!");
			System.exit(0);
		}
	}

	private String usage() {
		return ("\n"
				+ "Usage:  java -jar exp1dtree.jar [-f FRACTION] [-r RATIO]"
				+ " [-p STRATEGY] [-o OUTPUT] [-d DATA] [-t TEST]                  \n"
				+ "Options:\n"
				+ "    -f  Specifying the proportion of the training set           \n"
				+ "        50% is defualt                                          \n"
				+ "                                                                \n"
				+ "    -r  Specifying the ratio of positive to negative samples    \n"
				+ "        (1:)4 is defualt                                        \n"
				+ "                                                                \n"
				+ "    -p  Choosing pruning strategy, values of STRATEGY are:  	   \n"
				+ "        NO    No pruning (default)                              \n"
				+ "        PRE   Pre-pruning by size                               \n"
				+ "        POST  Post-pruning with 40% validation                  \n"
				+ "                                                                \n"
				+ "    -o  Specifying the output file name. stdout if omitted.     \n"
				+ "                                                                \n"
				+ "    -d  Specifying the training file name.                      \n"
				+ "        census-income.data.10000 if omitted.                    \n"
				+ "                                                                \n"
				+ "    -t  Specifying the test file name.                          \n"
				+ "        census-income.test.5000 if omitted.                     \n"
				+ "\n");
	}

	public InputStream getTrainInput() {
		return trainInput;
	}

	public InputStream getTestInput() {
		return testInput;
	}

	public PrintStream getOutput() {
		return output;
	}

	public PrintStream getErr() {
		return err;
	}
	
	public int getFraction() {
		return fraction;
	}
	
	public int getRatio() {
		return ratio;
	}
	
	public int getValidation() {
		return validation;
	}

	public DTree.Strategy getStrategy() {
		// TODO Auto-generated method stub
		return strategy;
	}
	
	
}
